﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;
using System.IO;
using Bitis_KeToanThue_Pro_DungChung.Common;
using Bitis_KeToanThue_Pro_DanhMuc.AccessDatabase;

namespace Bitis_KeToanThue_Pro_DanhMuc.Form
{
    public partial class frmDanhMucTaiSan : Telerik.WinControls.UI.RadForm
    {
        DanhMucTaiSan dmts = new DanhMucTaiSan();
        private int Matscd;
        private string matk="";
        private string madv="";
        public frmDanhMucTaiSan()
        {
            InitializeComponent();
        }
        private void khoitaoluoi()
        {
            var chon = new GridViewCheckBoxColumn();
            chon.HeaderText="chon";
            chon.Name = "chon";
            chon.Width = 50;
            chon.IsVisible = true;
            //chon.ReadOnly = true;
            grvTSCD.Columns.Add(chon);

            var matscd = new GridViewTextBoxColumn();
            matscd.HeaderText = "Mã tài sản";
            matscd.FieldName = "MaTSCD";
            matscd.Width = 100;
            matscd.ReadOnly = true;
            matscd.IsVisible = true;
            matscd.TextAlignment = ContentAlignment.MiddleCenter;
            grvTSCD.Columns.Add(matscd);

            var tentscd = new GridViewTextBoxColumn();
            tentscd.HeaderText = "Tên tài sản";
            tentscd.FieldName = "TenTSCD";
            tentscd.Width = 200;
            tentscd.ReadOnly = true;
            tentscd.IsVisible = true;
            tentscd.TextAlignment = ContentAlignment.MiddleCenter;
            grvTSCD.Columns.Add(tentscd);

            var dvt = new GridViewTextBoxColumn();
            dvt.HeaderText = "Đơn Vị Tính";
            dvt.FieldName = "DVT";
            dvt.Width = 100;
            dvt.ReadOnly = true;
            dvt.IsVisible = true;
            dvt.TextAlignment = ContentAlignment.MiddleCenter;
            grvTSCD.Columns.Add(dvt);

            var donvi = new GridViewTextBoxColumn();
            donvi.HeaderText = "Đơn Vị";
            donvi.FieldName = "MaDV";
            donvi.Width = 120;
            donvi.ReadOnly = true;
            donvi.IsVisible = true;
            donvi.TextAlignment = ContentAlignment.MiddleCenter;
            grvTSCD.Columns.Add(donvi);

            var ng_thuc_gs = new GridViewTextBoxColumn();
            ng_thuc_gs.HeaderText = "ng_thuc_gs";
            ng_thuc_gs.FieldName = "ng_thuc_gs";
            ng_thuc_gs.Width = 100;
            ng_thuc_gs.ReadOnly = true;
            ng_thuc_gs.IsVisible = true;
            ng_thuc_gs.TextAlignment = ContentAlignment.MiddleCenter;
            grvTSCD.Columns.Add(ng_thuc_gs);


            var sotk= new GridViewTextBoxColumn();
            sotk.HeaderText = "Số TK";
            sotk.FieldName = "SoTK";
            sotk.Width = 100;
            sotk.ReadOnly = true;
            sotk.IsVisible = true;
            sotk.TextAlignment = ContentAlignment.MiddleCenter;
            grvTSCD.Columns.Add(sotk);
        }
        private void loadgrid()
        {
            grvTSCD.DataSource = dmts.load_grid();
        }
        private void loadcombodv()
        {
            cboDV.DataSource = dmts.load_combodv();
            cboDV.DisplayMember = "TenDV";
            cboDV.ValueMember = "MaDV";
        }
        private void loadcombotk()
        {
            cboSoTK.DataSource = dmts.load_combotk();
            cboSoTK.DisplayMember = "ten_tk";
            cboSoTK.ValueMember = "ma_tk";
        }
        private void reset()
        {
            txtTenTSCD.Text = "";
            txtDVT.Text = "";
            txtng_thuc_gs.Text = "";
            txtDVT.Focus();
        }
        private void btThem_Click(object sender, EventArgs e)
        {
            //String DonVi = cboDV.SelectedValue.ToString();
            //String TaiKhoan = cboSoTK.SelectedValue.ToString();
            int ng_thu_gs = Convert.ToInt32(txtng_thuc_gs.Text);
            if(!string.IsNullOrEmpty(txtTenTSCD.Text) && !string.IsNullOrEmpty(txtDVT.Text))
            {
                if (dmts.them(txtTenTSCD.Text, txtDVT.Text, ng_thu_gs, ClassXuLyChung.GetSafeString(cboSoTK.SelectedValue), ClassXuLyChung.GetSafeString(cboDV.SelectedValue)) == 1)
                {
                    RadMessageBox.Show("Lưu thành công","Thông báo",MessageBoxButtons.OK,RadMessageIcon.Info);
                    reset();
                    loadgrid();
                }
            }
            else
            {
                RadMessageBox.Show("Bạn cần phải nhập đầy đủ thông tin","Thông báo",MessageBoxButtons.OK,RadMessageIcon.Info);
            }
        }

        private void btSoanLai_Click(object sender, EventArgs e)
        {
            reset();
        }

        private void frmDanhMucTaiSan_Load(object sender, EventArgs e)
        {
            khoitaoluoi();
            reset();
            loadcombodv();
            loadcombotk();
            loadgrid();
        }

        private void btSua_Click(object sender, EventArgs e)
        {
            //String DonVi=cboSoTK_SelectedIndexChanged();
            //String TaiKhoan=cboSoTK.SelectedValue.ToString();
            int ng_thu_gs = Convert.ToInt32(txtng_thuc_gs.Text);
            if(!string.IsNullOrEmpty(txtTenTSCD.Text) && !string.IsNullOrEmpty(txtDVT.Text))
            {
                if (dmts.sua(Matscd,txtTenTSCD.Text, txtDVT.Text, ng_thu_gs, matk,madv) == 1)
                {
                    RadMessageBox.Show("Update Thành công","Thông báo",MessageBoxButtons.OK,RadMessageIcon.Info);
                    reset();
                    loadgrid();
                }
            }
            else
            {
                RadMessageBox.Show("Bạn cần phải nhập đầy đủ thông tin","Thông báo",MessageBoxButtons.OK,RadMessageIcon.Info);
            }
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            DialogResult thongbao = RadMessageBox.Show("Bạn có chắc muốn xóa mục này", "Thông báo", MessageBoxButtons.YesNo, RadMessageIcon.Info);
            if(thongbao==DialogResult.Yes)
            {
                for (int i = 0; i < grvTSCD.Rows.Count; i++)
                {
                    if (grvTSCD.Rows[i].Cells[0].Value != null)
                    {
                        bool ischecked = (bool)grvTSCD.Rows[i].Cells[0].Value;
                        if (ischecked == true)
                        {
                            dmts.xoa(Matscd);
                        }
                    }
                }
                RadMessageBox.Show("Xóa thành công", "Thông báo", MessageBoxButtons.OK, RadMessageIcon.Info);
                reset();
                loadgrid();
            }

        }

        //private void grvTSCD_Click(object sender, EventArgs e)
        //{
        //    loadtextbox(grvTSCD.CurrentRow);
        //}
        private void loadtextbox(GridViewRowInfo hienthi)
        {
            if (hienthi != null )//&& !(hienthi is GridViewRowInfo))
            {
                txtTenTSCD.Text = ClassXuLyChung.GetSafeString(hienthi.Cells["TenTSCD"].Value);
                txtDVT.Text = ClassXuLyChung.GetSafeString(hienthi.Cells["DVT"].Value);
                txtng_thuc_gs.Text = ClassXuLyChung.GetSafeString(hienthi.Cells["ng_thuc_gs"].Value);
                //cboDV.Text = ClassXuLyChung.GetSafeString(hienthi.Cells["TTenS"].Value);
                //cboSoTK.Text = ClassXuLyChung.GetSafeString(hienthi.Cells["TenTS"].Value);
                Matscd = int.Parse(ClassXuLyChung.GetSafeString(hienthi.Cells["MaTSCD"].Value));
            }      
            else
            {
                reset();
            }
        }

        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            ClassXuLyChung.tuychontimkiemtrenluoi(txtTimKiem, grvTSCD, 1, 0);
        }

        private void grvTSCD_CellClick(object sender, GridViewCellEventArgs e)
        {
            loadtextbox(this.grvTSCD.CurrentRow);
        }

        private void cboDV_SelectedIndexChanged(object sender, EventArgs e)
        {
            //madv = cboSoTK_SelectedIndexChanged.sele;
        }

        private void cboSoTK_SelectedIndexChanged(object sender, EventArgs e)
        {
            //matk = ClassXuLyChung.GetSafeString(cboSoTK.SelectedValue);
        }

        private void radButton1_Click(object sender, EventArgs e)
        {
            Close();
        }

        //private void grvTSCD_Click(object sender, EventArgs e)
        //{
        //    loadtextbox(this.grvTSCD.CurrentRow);
        //}
    }
}
